
#include <physm.h>

static int _physm_alloc_mem;
static pthread_mutex_t _physm_mem_mutex;

void _physm_mem_add(int amount){

  pthread_mutex_lock(&_physm_mem_mutex);
  _physm_alloc_mem += amount;
  pthread_mutex_unlock(&_physm_mem_mutex);

}

void _physm_mem_sub(int amount){

  pthread_mutex_lock(&_physm_mem_mutex);
  _physm_alloc_mem -= amount;
  pthread_mutex_unlock(&_physm_mem_mutex);

}

int _physm_mem_get(){

  pthread_mutex_lock(&_physm_mem_mutex);
  int mem = _physm_alloc_mem;
  pthread_mutex_unlock(&_physm_mem_mutex);
  return mem;

}

/* Wrappers to the llist code to keep track of its memory usage. */

physm_body_t *physm_llist_add(llist_t *list, physm_body_t *data){

  _physm_mem_add(sizeof(llist_bucket_t));
  return (physm_body_t *) llist_add(list, (llist_data_t) data);

}

physm_body_t *physm_llist_get(llist_t *list, int offset){

  return (physm_body_t *) llist_get(list, offset);

}

physm_body_t *physm_llist_del(llist_t *list, int offset){

  _physm_mem_sub(sizeof(llist_bucket_t));
  return (physm_body_t *)llist_del(list, offset);

}

int physm_llist_len(llist_t *list){

  return llist_len(list);

}

void physm_llist_init(llist_t *list){

  llist_init(list);

}
